# waveplugs/wave.py
#
#

""" wave related commands. """

## gozerlib imports

from gozerlib.commands import cmnds
from gozerlib.examples import examples
from gozerlib.utils.exception import handle_exception
from gozerlib.persist.persist import PlugPersist
from gozerlib.callbacks import callbacks

## waveapi imports

from waveapi import document

## basic imports

import logging

def handle_waveclone(bot, event):

    if event.type != "wave":
        event.reply("this command only works in google wave.");
        return

    parts = list(event.root.GetParticipants())
    newwave = bot.newwave(event.context, parts)
    newwave.SetTitle(event.root.title)
    event.done()

cmnds.add('wave-clone', handle_waveclone, 'USER')
examples.add('wave-clone', 'clone the wave', 'wave-clone')

def handle_wavecontinue(bot, event):

    if event.type != "wave":
        event.reply("this command only works in google wave.");
        return

    parts = list(event.root.GetParticipants())
    newwave = bot.newwave(event.context, parts)
    newwave.SetTitle(event.root.title)

    rootblip = event.context.GetBlipById(newwave.GetRootBlipId())
    rootblip.GetDocument().AppendElement(event.rootblip)
    rootblip.GetDocument().AppendElement(
            document.Gadget('http://hb-bot.appspot.com/poll.xml'))

    event.done()

cmnds.add('wave-continue', handle_wavecontinue, 'OPER')
examples.add('wave-continue', 'continue this wave into a new wave', 'wave-continue')

def handle_wavenew(bot, event):

    if event.type != "wave":
        event.reply("this command only works in google wave.");
        return

    parts = ['hb-bot@appspot.com', event.userhost]
    newwave = bot.newwave(event.context, parts)

    if event.rest:
        newwave.SetTitle(event.rest)

    rootblip = event.context.GetBlipById(newwave.GetRootBlipId())
    rootblip.GetDocument().AppendElement(
            document.Gadget('http://hb-bot.appspot.com/poll.xml'))

    event.done()

cmnds.add('wave-new', handle_wavenew, 'USER')
examples.add('wave-new', 'make a new wave', 'wave-new')

def handle_wavepublic(bot, event):

    if event.type != "wave":
        event.reply("this command only works in google wave.");
        return

    event.root.AddParticipant('public@a.gwave.com')
    event.done()

cmnds.add('wave-public', handle_wavepublic, 'USER')
examples.add('wave-public', 'make the wave public', 'wave-public')

def handle_waveinvite(bot, event):
    if event.type != "wave":
        event.reply("this command only works in google wave.");
        return

    if not event.rest:
        event.missing('<who>')
        return

    event.root.AddParticipant(event.rest)
    event.done()

cmnds.add('wave-invite', handle_waveinvite, 'USER')
examples.add('wave-invite', 'invite a user/bot into the wave', 'wave-invite bthate@googlewave.com')

def handle_waveid(bot, event):
    if event.type != "wave":
        event.reply("this command only works in google wave.");
        return
    event.reply(event.waveid)

cmnds.add('wave-id', handle_waveid, 'USER')
examples.add('wave-id', 'show the id of the wave the command is given in.', 'wave-id')

def handle_waveurl(bot, event):
    if event.type != "wave":
        event.reply("this command only works in google wave.");
        return
    event.reply(event.url)

cmnds.add('wave-url', handle_waveurl, 'USER')
examples.add('wave-url', 'show the url of the wave the command is given in.', 'wave-url')

def handle_waveparticipants(bot, event):
    if event.type != "wave":
        event.reply("this command only works in google wave.");
        return
    event.reply("participants: ", list(event.root.GetParticipants()))

cmnds.add('wave-participants', handle_waveparticipants, 'USER')
examples.add('wave-participants', 'show the participants of the wave the command is given in.', 'wave-participants')

def handle_wavepart(bot, event):
    if event.type != "wave":
        event.reply("this command only works in google wave.");
        return

    event.reply('bye')
    event.root.RemoveSelf()

cmnds.add('wave-part', handle_wavepart, 'OPER')
examples.add('wave-part', 'leave the wave', 'wave-part')

continue_count = PlugPersist('continue-count')
continue_threshold = PlugPersist('continue-threshold')

def continue_callback(bot, event):

    try:
        continue_count.data[event.channel] += 1
        continue_count.save()
    except KeyError:
        continue_count.data[event.channel] = 1

    try:
        if continue_count.data[event.channel] > continue_threshold.data[event.channel]:
            continue_count.data[event.channel] = 0
            continue_count.save()
    except KeyError:
        return

    event.reply("this wave is being continue (threshold = %s)" % continue_threshold[event.channel])
    handle_waveclone(bot, event)

callbacks.add('BLIP_SUBMITTED', continue_callback)

def handle_wavethreshold(bot, event):

    if event.type != "wave":
        event.reply("this command only works in google wave.");
        return

    try:
        count = int(event.args[0])
    except (IndexError, ValueError):
        event.missing("<nrofblips>")
        return

    if not continue_count.data:
        continue_count.data = {}

    continue_count.data[event.channel] = count
    continue_count.save()
    event.done()

cmnds.add("wave-threshold", handle_wavethreshold, "OPER")
examples.add("wave-threshold", "set continue threshold of the wave .. after this many blips a new wave will be cloned.", "wave-threshold 200")

def handle_wavecontinue(bot, event):

    if event.type != "wave":
        event.reply("this command only works in google wave.");
        return

    # waiting for the new API

    event.done()

cmnds.add('wave-continue', handle_wavecontinue, 'OPER')
examples.add('wave-continue', 'continue this wave into a new wave', 'wave-continue')
